var searchParams = {};

/**
 * 初始化
 */
var InfoManage = {
    id: '#infoManageTable',	//表格id
    ajaxType: 'POST',		//查询类型
    url: 'getInfoManageList',
    dataList:[],
    table:null,
    searchTable:null,
    repeatTable:null,
    tableHtml:'<table id="infoManageTable" class="table table-border table-bordered table-bg table-hover table-sort table-responsive" style="width: 2700px;">\n' +
    '\t\t\t<thead>\n' +
    '\t\t\t<tr class="text-c">\n' +
    '\t\t\t\t<th width="25"><input type="checkbox" class="allCheckBox" onclick="InfoManage.selectAllCheckBox();"></th>\n' +
    '\t\t\t\t<th width="50">序号</th>\n' +
    '\t\t\t\t<th width="100">来电日期</th>\n' +
    '\t\t\t\t<th width="50">来电时间</th>\n' +
    '\t\t\t\t<th width="75">学生姓名</th>\n' +
    '\t\t\t\t<th width="80">信息状态</th>\n' +
    '\t\t\t\t<th width="100">预约时间</th>\n' +
    '\t\t\t\t<th width="90">考虑入学时间</th>\n' +
    '\t\t\t\t<th width="">咨询内容</th>\n' +
    '\t\t\t\t<th width="90">联系电话1</th>\n' +
    '\t\t\t\t<th width="75">家长姓名1</th>\n' +
    '\t\t\t\t<th width="90">联系电话2</th>\n' +
    '\t\t\t\t<th width="75">家长姓名2</th>\n' +
    '\t\t\t\t<th width="100">现读学校</th>\n' +
    '\t\t\t\t<th width="80">现读年级</th>\n' +
    '\t\t\t\t<th width="120">家庭住址</th>\n' +
    '\t\t\t\t<th width="100">信息来源</th>\n' +
    '\t\t\t\t<th width="100">特别备注</th>\n' +
    '\t\t\t\t<th width="60">数学</th>\n' +
    '\t\t\t\t<th width="60">数学等级</th>\n' +
    '\t\t\t\t<th width="60">英语</th>\n' +
    '\t\t\t\t<th width="60">英语等级</th>\n' +
    '\t\t\t\t<th width="60">物化</th>\n' +
    '\t\t\t\t<th width="60">物化等级</th>\n' +
    '\t\t\t\t<th width="60">跟进人</th>\n' +
    '\t\t\t\t<th width="85">最后修改时间</th>\n' +
    '\t\t\t\t<th width="85">操作</th>\n' +
    '\t\t\t</tr>\n' +
    '\t\t\t</thead>\n' +
    '\t\t\t<tbody>\n' +
    '\t\t\t</tbody>\n' +
    '\t\t</table>'
};

$(function(){
    InfoManage.defaultList();
});

InfoManage.defaultList = function () {
    InfoManage.infoList();
}

InfoManage.searchList = function () {
    InfoManage.url = 'searchInfoManages';  //请求地址
    InfoManage.infoList();
}

InfoManage.repeatList = function () {
    InfoManage.url = 'getRepeatInfos';  //请求地址
    InfoManage.infoList();
}

InfoManage.infoList = function () {
    $('#list').html(InfoManage.tableHtml);
    InfoManage.table = $(InfoManage.id).tbList({
        //加载列数据
        ajaxType:InfoManage.ajaxType,	//请求方式
        url:InfoManage.url,	//请求地址
        searchParams:null2str(InfoManage.formParams()),//搜索条件
        columnDefs: InfoManage.columnDefs(),//行操作按钮
        columns: InfoManage.initColumn()	//行数据
    });

    // 表格重绘时
    InfoManage.table.DataTable().on('draw.dt', function () {
        // 分权限显示
        // 全选复选框
        //$("#allboxs").prop("checked", false);
        // 序号
        InfoManage.table.DataTable().column(1, {
            search: "applied",
            order: "applied"
        }).nodes().each(function (cell, i) {
            cell.innerHTML = i + 1;
        });
    });
}


/**
 * 初始化表格的列
 */
InfoManage.initColumn = function () {
    return [	//行数据
        {"data":null,"orderable":false,"render": function (data, type, row, meta) {
            var btn = '';
            btn +=  '<input type="checkbox" value="'+row.id+'" followUserId="'+row.followPersonId+'" name="selectCheckBox"/>';
            return btn;
        }},
        { "data": null ,"orderable":false , "className": "text-c" },
        { "data": "callTime" ,"orderable":false,"render": function (data, type, row, meta) {
            if(row.callTime!=null && row.callTime!=''){
                return row.callTime.substring(0, 10);
            }else return '';
        }},
        { "data": "callTime","orderable":false ,"render": function (data, type, row, meta) {
            if(row.callTime!=null && row.callTime!=''){
                return row.callTime.substring(11, 19);
            }else return '';
        } },
        { "data": "studentName","orderable":false },
        { "data": "infoState","orderable":false,"render": function (data, type, row, meta) {
            //var btn = '';
            var infoStateName = map[row.infoState];
            if(infoStateName!=null){
                return map[row.infoState];
            }else{
                return row.infoState;
            }

        } },
        { "data": "appointment","orderable":false ,"render": function (data, type, row, meta){
            if(row.appointment!=null && row.appointment!=''){
                var dateStr = row.callTime.substring(0, 10);
                var hour = row.callTime.substring(11, 12);
                var hourStr = ' 上午';
                if(12<parseInt(hour) && parseInt(hour)<24){
                    hourStr = ' 下午';
                }
                return dateStr+hourStr;
            }else return '';

        } },
        { "data": "considerAdmissionTime","orderable":false  },

        { "data": "consultationContent","orderable":false  },
        { "data": "contactNumMain","orderable":false  },
        { "data": "parentNameMain","orderable":false  },
        { "data": "contactNumOther","orderable":false  },
        { "data": "parentNameOther","orderable":false  },
        { "data": "currentSchool","orderable":false  },
        { "data": "currentGrade","orderable":false  },
        { "data": "homeAddress","orderable":false  },
        { "data": "infoSource","orderable":false  },
        { "data": "remark","orderable":false ,"render": function (data, type, row, meta) {
            return '<span class="c-red">'+row.remark+'</span>';
        }  },
        { "data": "mathScore","orderable":false  },
        { "data": "mathGrade","orderable":false  },
        { "data": "englishScore","orderable":false  },
        { "data": "englishGrade","orderable":false  },
        { "data": "phyChemScore","orderable":false  },
        { "data": "phyChemGrade","orderable":false  },
        /*{ "data": "createUserId","orderable":false  },*/
        { "data": "followPersonName","orderable":false  },
        { "data": "modifyTime","orderable":false  }

    ];
};


/*
* 按钮控制
* */
InfoManage.columnDefs = function () {
    InfoManage.dataList=[];
    return [

        {
            "targets": 26, //列的样式名
            "orderable": false, //包含上样式名‘nosort'的禁止排序
            "render": function (data, type, row, meta) {
                //console.log('row');
                //console.log(row);
                var btn = '';
                btn +=  '<a title="编辑" href="javascript:;" onclick="InfoManage.editInfoManage(\'角色编辑\',\'editInfoManage\','+row.id+')" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a>';
                btn +=  '<a title="删除" href="javascript:;" onclick="InfoManage.delInfoManage(this,'+ row.id +')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a>';
                //btn += ' <a title="查看" href="javascript:;"  onclick="InfoManage.view(' + row.id + ')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">查看</i></a>';
                return btn;
            }

        }];
};

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
InfoManage.formParams = function() {
    //searchParams.infoState=$('input[name="infoState"]').val();
    console.log($('input[name="mobile"]').val())
    //searchParams.mobile=$('input[name="mobile"]').val();
    //searchParams.name=$('input[name="name"]').val();


    searchParams = {
       // 'infoState':null,
        'id':$('input[name="id"]').val(),
        'mobile':$('input[name="mobile"]').val(),
        'name':$('input[name="name"]').val()
    };
    return searchParams;
}

/*添加*/
InfoManage.addInfoManage = function(title,url,w,h){
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}

/*资讯-编辑*/
InfoManage.editInfoManage = function (title,url,id,w,h){
    var index = layer.open({
        type: 2,
        title: title,
        content: url+"/"+id
    });
    layer.full(index);
}


InfoManage.searchInfoManages = function (){

    if(InfoManage.url == 'searchInfoManages'){
        InfoManage.formParams();
        null2str(searchParams);//转换数据
        InfoManage.table.api().ajax.reload();
    }else{
        InfoManage.searchList();
    }
}

InfoManage.repeatInfoManages = function (){
    //$('.search').click(function () {
    // 搜索预处理

    var count = 0;
    var id;
    var followUserId;
    $('input[name="selectCheckBox"]:checked').each(function(i, item){
        id = $(this).val();
        followUserId = $(this).attr('followUserId');
        count++;
    });
    if(count==1){
        $('#id').val(id);
        null2str(searchParams);//转换数据
        if(InfoManage.url == 'getRepeatInfos'){
            InfoManage.formParams();
            null2str(searchParams);//转换数据
            InfoManage.table.api().ajax.reload();
        }else{
            InfoManage.repeatList();
        }
    }else{
        alert('请选择其中一条数据');
    }
}



/*导入文件*/
InfoManage.uploadInfoManage = function (){
    layer.open({
        type: 2,
        area: ['780px','410px'],
        fix: false, //不固定
        maxmin: true,
        shade:0.4,
        title: '导入文件',
        content: 'uploadInfoManage'
    });
}

/*导出文件*/
InfoManage.exportInfoManage = function (){
    var head = window.parent.indexParam.getHead();
    window.location.href = "exportInfosFile/"+head.userId;
}

/*跟进人*/
InfoManage.followUpInfoManage = function (){
    var count = 0;
    var id;
    var followUserId;
    $('input[name="selectCheckBox"]:checked').each(function(i, item){
        id = $(this).val();
        followUserId = $(this).attr('followUserId');
        count++;
    });
    if(count==1){
        layer.open({
            type: 2,
            area: ['780px','410px'],
            fix: false, //不固定
            maxmin: true,
            shade:0.4,
            title: '转移跟进人',
            content: 'followInfoManage/'+id+'/'+followUserId
        });
    }else{
        alert('请选择其中一条数据');
    }
}

/*预约提醒*/
InfoManage.remindInfoManage = function (){
    layer.open({
        type: 2,
        area: ['780px','410px'],
        fix: false, //不固定
        maxmin: true,
        shade:0.4,
        title: '预约提醒',
        content: '../infoManage/remind'
    });
}

/*删除*/
InfoManage.delInfoManage = function (obj,id){
    var myMsg = confirm('确认要删除吗？');
    if(!myMsg){
        return false;
    }
    var ids = parseInt(id);
    var body = {'infoId':[ids]};
    var data={"head": window.parent.parent.indexParam.getHead(),"data":body};
    $.ajax({
        type: 'POST',
        url:window.parent.parent.indexParam.getUrl('deleteInfoMange'),
        data:JSON.stringify(data),
        contentType:"application/json",  //缺失会出现URL编码，无法转成json对象
        success: function(result){
            //layer.msg('已删除!',{icon:1,time:1000});
            if(result.head.respStatus=="00"){
                $(obj).parents("tr").remove();
                alert(result.head.respDesc);
            }else{
                alert(result.head.respDesc);
            }
        },
        error:function(data) {
            console.log(data.msg);
        },
    });
}



InfoManage.delAllInfoManage=function(){
    var myMsg = confirm('确认要删除吗？');
    if(!myMsg){
        return false;
    }
    var count = 0;
    var ids = [];
    $('input[name="selectCheckBox"]:checked').each(function(i, item){
        ids.push(parseInt($(this).val()));
        count++;
    });

    if(count==0){
        alert('选择要删除的数据！');
        return false;
    }

    var body = {'infoId':ids};
    var data={"head": window.parent.parent.indexParam.getHead(),"data":body};
    $.ajax({
        type: 'POST',
        url:window.parent.parent.indexParam.getUrl('deleteInfoMange'),
        data:JSON.stringify(data),
        contentType:"application/json",  //缺失会出现URL编码，无法转成json对象
        success: function(result){
            //layer.msg('已删除!',{icon:1,time:1000});
            if(result.head.respStatus=="00"){
                $('input[name="selectCheckBox"]:checked').each(function(i, item){
                    $(this).parents("tr").remove();
                });
                alert(result.head.respDesc);
            }else{
                alert(result.head.respDesc);
            }
        },
        error:function(data) {
            console.log(data.msg);
        },
    });
}


InfoManage.selectAllCheckBox=function(){
    //console.log("checked:"+$('.allCheckBox').prop("checked"))
    $('input[name="selectCheckBox"]').each(function(i, item){
        if($('.allCheckBox').prop("checked")){
            $(this).prop("checked", true);
        }else{
            $(this).prop("checked", false);
        }
    });
}

function aa(stateName) {
    $("#stateName").val(stateName);
}

/*审核*/
function article_shenhe(obj,id){
    layer.confirm('审核文章？', {
            btn: ['通过','不通过','取消'],
            shade: false,
            closeBtn: 0
        },
        function(){
            $(obj).parents("tr").find(".td-manage").prepend('<a class="c-primary" onClick="article_start(this,id)" href="javascript:;" title="申请上线">申请上线</a>');
            $(obj).parents("tr").find(".td-status").html('<span class="label label-success radius">已发布</span>');
            $(obj).remove();
            layer.msg('已发布', {icon:6,time:1000});
        },
        function(){
            $(obj).parents("tr").find(".td-manage").prepend('<a class="c-primary" onClick="article_shenqing(this,id)" href="javascript:;" title="申请上线">申请上线</a>');
            $(obj).parents("tr").find(".td-status").html('<span class="label label-danger radius">未通过</span>');
            $(obj).remove();
            layer.msg('未通过', {icon:5,time:1000});
        });
}
/*资讯-下架*/
function article_stop(obj,id){
    layer.confirm('确认要下架吗？',function(index){
        $(obj).parents("tr").find(".td-manage").prepend('<a style="text-decoration:none" onClick="article_start(this,id)" href="javascript:;" title="发布"><i class="Hui-iconfont">&#xe603;</i></a>');
        $(obj).parents("tr").find(".td-status").html('<span class="label label-defaunt radius">已下架</span>');
        $(obj).remove();
        layer.msg('已下架!',{icon: 5,time:1000});
    });
}

/*资讯-发布*/
function article_start(obj,id){
    layer.confirm('确认要发布吗？',function(index){
        $(obj).parents("tr").find(".td-manage").prepend('<a style="text-decoration:none" onClick="article_stop(this,id)" href="javascript:;" title="下架"><i class="Hui-iconfont">&#xe6de;</i></a>');
        $(obj).parents("tr").find(".td-status").html('<span class="label label-success radius">已发布</span>');
        $(obj).remove();
        layer.msg('已发布!',{icon: 6,time:1000});
    });
}
/*资讯-申请上线*/
function article_shenqing(obj,id){
    $(obj).parents("tr").find(".td-status").html('<span class="label label-default radius">待审核</span>');
    $(obj).parents("tr").find(".td-manage").html("");
    layer.msg('已提交申请，耐心等待审核!', {icon: 1,time:2000});
}

